package com.huidu.bitrade.util;

import com.alibaba.fastjson2.util.DateUtils;
import com.huidu.bitrade.entity.ExchangeTrade;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Data
public class NettyPushDataUtil {

    @Value("${netty.push.url}")
    private String url;

    /**
     * @param symbol
     * @param data {@link com.huidu.bitrade.entity.ExchangeTrade}
     */
    public void pushMarketTrade(String symbol, Object data){
        ServiceData build = ServiceData.builder()
                .context(data)
                .service("market-price-trade")
                .symbol(symbol)
                .date(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSSS") + ":").build();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, build, String.class);
        log.info("发送挂单数据：{}",stringResponseEntity.getBody());
    }

    public void pushMarketPlate(String symbol, Object object){
        ServiceData build = ServiceData.builder()
                .context(object)
                .service("market-price-plate")
                .symbol(symbol)
                .date(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSSS") + ":").build();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, build, String.class);
        log.info("发送盘口数据：{}",stringResponseEntity);
    }

    public void pushMarketDepth(String symbol, Object object){
        ServiceData build = ServiceData.builder()
                .context(object)
                .service("market-price-depth")
                .symbol(symbol)
                .date(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSSS") + ":").build();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, build, String.class);
        log.info("发送深度数据：{}",stringResponseEntity);
    }

    public void pushMarketKline(String symbol, Object object){
        ServiceData build = ServiceData.builder()
                .context(object)
                .service("market-kline")
                .symbol(symbol)
                .date(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSSS") + ":").build();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, build, String.class);
        log.info("发送k线数据：{}",stringResponseEntity);
    }

    public void pushMarketSpot(String symbol, Object object){
        ServiceData build = ServiceData.builder()
                .context(object)
                .service("spot-market-price")
                .symbol(symbol)
                .date(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSSS") + ":").build();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, build, String.class);
        log.info("发送现货行情数据：{}",stringResponseEntity);
    }

    public static void main(String[] args) {
        NettyPushDataUtil util = new NettyPushDataUtil();
        util.setUrl("http://127.0.0.1:8080/sendMessage");
        List<ExchangeTrade> list = new ArrayList<>();
        ExchangeTrade exchangeTrade = new ExchangeTrade();
        exchangeTrade.setAmount(BigDecimal.ZERO);
        exchangeTrade.setSymbol("ALEO/CNHD");
        list.add(exchangeTrade);

        ExchangeTrade exchangeTrade1 = new ExchangeTrade();
        exchangeTrade1.setAmount(BigDecimal.ZERO);
        exchangeTrade1.setSymbol("ALEO/CNHD");
        list.add(exchangeTrade);

        util.pushMarketTrade("ALEO/CNHD",list);
    }


    @Data
    @Builder
    public static class ServiceData{
        /**
         * 具体的数据
         */
        private Object context;

        /**
         * 参考常量
         */
        private String service;

        private String symbol;

        private String date;
    }
}
